﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;


namespace DeltaFlex.Data
{
    public class Operacions_BD
    {
        public static SqlConnection CrearConnexio()
        {
            try
            {
                string conStr = ConfigurationManager.ConnectionStrings["DeltaFlexConnectionString"].ConnectionString;
                SqlConnection con = new SqlConnection();
                con.ConnectionString = conStr;
                con.Open();
                return con;
            }
            catch { throw; }
        }

        #region Inserts
        public static int Insertar_RetornaPrimer(SqlCommand cmd)
        {
            cmd.Connection = CrearConnexio();
            int primer = 0;
            try
            {
                cmd.ExecuteNonQuery();
                cmd.Connection.Close();
                primer = Int32.Parse(cmd.Parameters[0].Value.ToString());
            }
            catch { throw; }
            return primer;
        }

        public static int Insertar(SqlCommand cmd)
        {
            cmd.Connection = CrearConnexio();
            int resposta = 0;

            try
            {
                resposta = cmd.ExecuteNonQuery();
                cmd.Connection.Close();
            }
            catch { throw; }
            return resposta;
        }
        #endregion

        #region Selects
        public static IDataReader Reader(SqlCommand cmd)
        {
            IDataReader reader = null;

            cmd.Connection = CrearConnexio();
            try
            {
                reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                return reader;
            }
            catch { throw; }
        }

        #endregion

        #region Updates
        public static void Update(SqlCommand cmd)
        {
            cmd.Connection = CrearConnexio();
            try
            {
                cmd.ExecuteNonQuery();
                cmd.Connection.Close();
            }
            catch { throw; }

        }

        #endregion
    }
}